package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;

import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.DataItemResponse;
import com.easycarebn.request.data.DoctorDetail;
import com.easycarebn.request.data.UserResponse;
import com.easycarebn.service.request.OnRequest;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class FragmentSendComment extends Fragment {

    private View mView;
    private static FragmentSendComment fragment = null;
    private static LinearLayout linesendCmt;
    private static View btnDangnhapCmt;
    private ImageView imgAvatar;
    private  UserResponse user;

    public static FragmentSendComment newInstance(String title) {
            fragment = new FragmentSendComment();
        return fragment;
    }

    public void update() {
        btnDangnhapCmt.setVisibility(View.GONE);
        linesendCmt.setVisibility(View.VISIBLE);
        //user = Utils.getUserLogin(DetailtActivity.activity);
       // Picasso.with(getActivity()).load(user.getUser().getAvatar()).resize(Utils.IMG_WIDHT, Utils.IMG_HEIGHT).placeholder(R.drawable.avata).error(R.drawable.avata).into(imgAvatar);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_send_comment, null);

        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Utils.setOnTouchHideKeyboard( getActivity(),mView);
        imgAvatar = (ImageView) mView.findViewById(R.id.img_avata);
        View btnSend = mView.findViewById(R.id.btnSend);
        btnDangnhapCmt = mView.findViewById(R.id.btnDangnhapCmt);
        btnDangnhapCmt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.intentToDetailt(getActivity(), Utils.LOGIN_CMT);

            }
        });

        linesendCmt = (LinearLayout) mView.findViewById(R.id.lineSendCmt);
        user = Utils.getUserLogin(getActivity());
        if (user == null || user.getUser() == null) {
            linesendCmt.setVisibility(View.GONE);
            btnDangnhapCmt.setVisibility(View.VISIBLE);
        } else {
            //Picasso.with(getActivity()).load(user.getUser().getAvatar()).resize(Utils.IMG_WIDHT, Utils.IMG_HEIGHT).placeholder(R.drawable.avata).error(R.drawable.avata).into(imgAvatar);
            Log.d("AccountAva",user.getUser().getAvatar());
        	Picasso.with(getActivity())
			.load(user.getUser().getAvatar())
			.resize(Utils.IMG_WIDHT, Utils.IMG_HEIGHT)
			.placeholder(R.drawable.avata).error(R.drawable.avata)
			.into(imgAvatar);
        }

        final RatingBar rateCommon = (RatingBar) mView.findViewById(R.id.rateCamnhanchung);
        final RatingBar rateFacilities = (RatingBar) mView.findViewById(R.id.rateCosovatchat);
        final RatingBar rateWaitingTime = (RatingBar) mView.findViewById(R.id.rateThoigiancho);
        final EditText edComment = (EditText) mView.findViewById(R.id.ed_comment);
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String content = edComment.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    Utils.showMessage(getActivity(), R.string.ban_chua_nhap_noi_dung_binh_luan);
                    return;
                }
                DoctorDetail doctor = FragmentDrDetailt.newInstance("").getCurrentDoctor();
                if (doctor == null) {
                    return;
                }
                ServerController.getInstance(getActivity()).requestPostDoctorComments(user.getUser().getId(),
                        doctor.getDoctorId(), user.getToken(), content, (int) rateCommon.getRating(),
                        (int) rateFacilities.getRating(), (int) rateWaitingTime.getRating(), new OnRequest<DataItemResponse>() {

                            @Override
                            public void onSuccess(DataItemResponse data) {
                                Utils.showMessage(getActivity(), R.string.ban_da_gui_binh_luan_thanh_cong);
                            }

                            @Override
                            public void onError(int errorCode, String message) {
                                Utils.showMessage(getActivity(), message);
                            }
                        });
            }
        });
    }
}
